package com.ict.ms.artificial.service.rabbitmq;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;

/**
 * Created by chenlong on 2016/12/14.
 */
public class ProperUtils_Artificial {
    public String host;
    public String user;
    public String passWord;
    public String exchange;

    public String queue;
    public String routingKey;
    public String bindingKey;

    public int port;

    public int getPort() {
        return port;
    }

    public String getQueue() {
        queue=exchange+"."+getRandomString(10);
        return queue;
    }

    public String getHost() {
        return host;
    }

    public String getUser() {
        return user;
    }

    public String getPassWord() {
        return passWord;
    }

    public String getExchange() {
        return exchange;
    }

    public String getRoutingKey() {
        return routingKey;
    }

    public String getBindingKey() {
        return bindingKey;
    }

    public ProperUtils_Artificial(){
        Properties prop =  new  Properties();
        InputStream ins = this.getClass().getResourceAsStream("/rmqArtificial.properties");
        try  {
            prop.load(ins);

            host =prop.getProperty("host");
            user = prop.getProperty("user");
            passWord =prop.getProperty("passWord");
            exchange =prop.getProperty("exchange");
            routingKey=prop.getProperty("routingKey");
            bindingKey=prop.getProperty("bindingKey");
            port=Integer.parseInt(prop.getProperty("port"));

            System.out.println("RabbitMQ:"+host+","+user+","+passWord+","+exchange+","+routingKey+","+bindingKey+","+port);
            close(ins);
        }  catch  (IOException e) {
            close(ins);
            e.printStackTrace();
        }
    }

    private void close(InputStream ins){
        try{
            ins.close();
        }catch (Exception e){
            System.out.println("输入流关闭异常："+e);
        }
    }

    /**
     * 用于生成随机队列名
     * @param length  随机队列名长度
     * @return  随机队列名
     */
    public String getRandomString(int length){
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//含有字符和数字的字符串
        Random random = new Random();//随机类初始化
        StringBuffer sb = new StringBuffer();//StringBuffer类生成，为了拼接字符串

        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(62);// [0,62)

            sb.append(str.charAt(number));
        }
        return "Server_Artificial"+sb.toString();
    }

//        //获取项目相对路径
//        public String getFileRoad(){
//            String path1 = Thread.currentThread()
//                    .getContextClassLoader().getResource("").getPath();
//
//            return path1;
//        }
}
